package com.dlut.controller;

import com.dlut.pojo.BasicData;
import com.dlut.service.IBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/basic")
public class BasicController {

    @Autowired
    private IBasicService service;

    @RequestMapping("/query")
    public String query(BasicData data, Model model) throws Exception {
        List<BasicData> list = service.query(data);
        model.addAttribute("list", list);
        return "basic/basic";
    }

    @RequestMapping("/basicDispatch")
    public String baseDispatch(Integer id, Model model) throws Exception {
        if (id != null){
            //更新
            BasicData basicData = service.queryById(id);
            model.addAttribute("basicData", basicData);
        }

        List<BasicData> parents = service.queryAllParentData();
        model.addAttribute("parents", parents);

        return "basic/updateBasic";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(BasicData data) throws Exception {
        if (data.getBaseId() != null && data.getBaseId() > 0){
            service.updateBasicData(data);
        }
        else {
            service.addBasicData(data);
        }

        return "redirect:/basic/query";
    }
}
